﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TraCuu_Update
{
    public partial class SachGUI : UserControl, 
        GUI.UctrlGUI, GUI.TraCuu_Update.GVPageInf
    {
        GUI.TraCuu_Update.GVPagesGUI _List = null;
        List<GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn> _columns = null;
        BLL.DanhSachSachBLL _DanhSachSachBLL = null;
        String[] _filter = null;
        bool _EnableEdit = false;
        Action<DTO.DauSachDTO> _Callback = null;

        public SachGUI(bool edit, Action<DTO.DauSachDTO> callback)
        {
            InitializeComponent();

            _filter = new String[] { 
                String.Empty, // ten
                String.Empty, // theloai
                String.Empty, // tacgia
                String.Empty  // luongton
            };
            _DanhSachSachBLL = new BLL.DanhSachSachBLL();
            _EnableEdit = edit;
            _Callback = callback;
        }

        public String getName()
        {
            if (_EnableEdit == true)
                return "CẬP NHẬT SÁCH";
            return "TRA CỨU SÁCH";
        }

        public int getPages()
        {
            _filter[0] = txt_TenSach.Text;
            _filter[1] = txt_TheLoai.Text;
            _filter[2] = txt_TacGia.Text;
            _filter[3] = txt_LuongTon.Text;

            return _DanhSachSachBLL.CountMatchFilter(_filter);
        }

        public List<GUI.TraCuu_Update.GVPagesGUI.GVPagesItem> getItemsPerPage(int page)
        {
            List<DTO.DauSachDTO> sachs = 
                _DanhSachSachBLL.MatchFilterPerPage(page, _filter);
            List<GUI.TraCuu_Update.GVPagesGUI.GVPagesItem> list = 
                new List<GUI.TraCuu_Update.GVPagesGUI.GVPagesItem>();
            foreach (DTO.DauSachDTO dto in sachs)
            {
                GUI.TraCuu_Update.GVPagesGUI.GVPagesItem item = new GUI.TraCuu_Update.GVPagesGUI.GVPagesItem();
                item.Data = dto.ToStringList();
                list.Add(item);
            }
            return list;
        }

        public void processItemChange(List<GUI.TraCuu_Update.GVPagesGUI.GVPagesItem> items)
        {
            List<DTO.DauSachDTO> itemsChage = new List<DTO.DauSachDTO>();
            foreach (GUI.TraCuu_Update.GVPagesGUI.GVPagesItem item in items)
            {
                if (item.Edited == true)
                {
                    DTO.DauSachDTO dto = new DTO.DauSachDTO(item.Data);
                    itemsChage.Add(dto);
                }
            }
            _DanhSachSachBLL.UpdateList(itemsChage);
        }

        private void SachGUI_Load(object sender, EventArgs e)
        {
            txt_LuongTon.Text = String.Empty;
            txt_TacGia.Text = "*";
            txt_TenSach.Text = "*";
            txt_TheLoai.Text = "*";
            //txt_LuongTon.Text = ">=300";
            InitColums();
            pnl_List.Controls.Clear();
            pnl_List.Controls.Add(_List);
        }

        private void InitColums()
        {
            _columns = new List<GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn>();

            DataGridViewColumn clID =
                new DataGridViewColumn();
            clID.Name = "ID";
            clID.HeaderText = "ID";
            clID.Visible = false;
            clID.ReadOnly = true;
            _columns.Add(new GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn(clID,null));

            DataGridViewColumn clTenSach = 
                new DataGridViewColumn();
            clTenSach.Name = "TenSach";
            clTenSach.HeaderText = "Tên Sách";
            clTenSach.Visible = true;
            clTenSach.ReadOnly = !_EnableEdit;
            _columns.Add(new GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn(clTenSach, null));

            DataGridViewColumn clTheLoai = 
                new DataGridViewColumn();
            clTheLoai.Name = "TheLoai";
            clTheLoai.HeaderText = "Thể Loại";
            clTheLoai.Visible = true;
            clTheLoai.ReadOnly = !_EnableEdit;
            _columns.Add(new GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn(clTheLoai, null));

            DataGridViewColumn clTacGia = 
                new DataGridViewColumn();
            clTacGia.Name = "TacGia";
            clTacGia.HeaderText = "Tác Giả";
            clTacGia.Visible = true;
            clTacGia.ReadOnly = !_EnableEdit;
            _columns.Add(new GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn(clTacGia, null));

            DataGridViewColumn clSoLuong = 
                new DataGridViewColumn();
            clSoLuong.Name = "SoLuong";
            clSoLuong.HeaderText = "Số Lượng";
            clSoLuong.Visible = true;
            clSoLuong.ReadOnly = !_EnableEdit;
            if (clSoLuong.ReadOnly == true)
                _columns.Add(new GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn(clSoLuong, null));
            else
                _columns.Add(new GUI.TraCuu_Update.GVPagesGUI.GVPagesColumn(clSoLuong, txt_LuongTon_Edit_KeyPress));

            _List = new GUI.TraCuu_Update.GVPagesGUI(this, _columns, ProcessCallBack);
        }

        private void btn_Tim_Click(object sender, EventArgs e)
        {
            _List.Reload();
        }

        private void ProcessCallBack(List<String> l)
        {
            if (_Callback != null)
            {
                DTO.DauSachDTO dto = new DTO.DauSachDTO(l);
                _Callback(dto);
            }
        }

        private void txt_LuongTon_Edit_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) &&
                !char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void txt_LuongTon_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) &&
                !char.IsDigit(e.KeyChar) &&
                e.KeyChar != '=' &&
                e.KeyChar != '>' &&
                e.KeyChar != '<')
                e.Handled = true;

            if (e.KeyChar == '=' &&
                (sender as TextBox).Text.IndexOf('=') > -1)
                e.Handled = true;
            if (e.KeyChar == '>' &&
                (sender as TextBox).Text.IndexOf('>') > -1)
                e.Handled = true;
            if (e.KeyChar == '<' &&
                (sender as TextBox).Text.IndexOf('<') > -1)
                e.Handled = true;
        }

        public void SetCallback(Action<DTO.DauSachDTO> callback)
        {
            _Callback = callback;
        }
    }
}
